<?php

/**
 * upload actions.
 *
 * @package    PHOTO_GALLERY
 * @subpackage upload
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class uploadActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->album = new AlbumsForm();
        $this->album->setDefault('owner_id', $this->getUser()->getAttribute('userId'));
        $this->imgForm1 = new ImagesForm();
        $this->imgForm1->setDefault('order_in_album', 0);
        $this->imgForm2 = new ImagesForm();
        $this->imgForm2->setDefault('order_in_album', 1);
        $this->imgForm3 = new ImagesForm();
        $this->imgForm3->setDefault('order_in_album', 2);
    }

    public function executeCreateAlbum(sfWebRequest $request) {

        $formInput = $request->getParameter('albums');
        $album = new AlbumsForm();
        $album->bind($formInput);
        if ($album->isValid()) {
            $album = new Albums();
            $album->setOwnerId($this->getUser()->getAttribute('userId'));
            $album->setName($formInput['name']);
            $album->setPublic(1);
            if ($request->getParameter('private', false)) {
                $album->setPublic(0);
            }
            $album->save();
        }
        return $this->renderText($album->getAlbumId());
    }

    public function executeUploadFile(sfWebRequest $request) {


        $formInput = $request->getParameter('image');
        $this->imgForm = new ImagesForm();
//        var_dump($formInput);
        $this->imgForm->bind($formInput, $request->getFiles('image'));
        
        if ($this->imgForm->isValid()) {

            $file = $this->imgForm->getValue('file');
            
            $img = new Images();
            $img->setAlbumId($formInput['album_id']);
            $img->setNoOfRates(0);
            $img->setAvgRate(0);
            $img->setPrice($formInput['price']);
            $img->setName($formInput['name']);
            $img->setOrderInAlbum($formInput['order_in_album']);
            $img->setSize( $file->getSize() /(1024) );

            $img->save();
            

            $file = $this->imgForm->getValue('file');
            $filename = $img->getImageId();
            $file->save(sfConfig::get('sf_upload_dir') . '/' . $filename);

            return $this->renderText($img->getImageId() . ";");
//            echo 'saved';
        }
        return $this->renderText($this->imgForm->getErrorSchema());
    }

}
